// https://leetcode.cn/problems/convert-a-number-to-hexadecimal/
// Created by ade on 2022/8/5.
//
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

class Solution {
public:
    // 这种方式只能适用于整数，不通用
    string toHex1(int num) {
        string a = "";
        while (num > 15) {
            int tmp = num % 15;
            num = (num - tmp) / 15;
            if (tmp < 10) {
                a += tmp + '0';
            } else {
                a += tmp - 11 + 'a';
            }
        }
        a += num + '0';
        reverse(a.begin(), a.end());
        return a;
    }

    string toHex2(int num) {
        if (num == 0) return "0";
        string hex = "0123456789abcdef", ans = "";
        while (num && ans.size() < 8) {
            cout << (num & 0xf) << endl;
            ans = hex[(num & 0xf)] + ans;
            num >>= 4;
        }
        return ans;
    }

    string toHex(int num) {
        stringstream ss;
        string ans;
        ss << hex << num;
        ss >> ans;
        return ans;
    }
};

int main() {
    int a = 27;
    Solution so;
    cout << so.toHex(a) << endl;
    return 0;
}